package cn.wjx.taobaounion.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.base.BaseFragment;
import cn.wjx.taobaounion.model.domain.Categories;
import cn.wjx.taobaounion.presenter.IHomePresenter;
import cn.wjx.taobaounion.ui.activity.MainActivity;
import cn.wjx.taobaounion.ui.activity.ScannerQRActivity;
import cn.wjx.taobaounion.ui.adapter.HomePagerAdapter;
import cn.wjx.taobaounion.utils.LogUtils;
import cn.wjx.taobaounion.utils.PresenterManger;
import cn.wjx.taobaounion.view.IHomeCallback;

/**
 * @author WuChangJian
 * @date 2020/6/16 21:07
 */
public class HomeFragment extends BaseFragment implements IHomeCallback {

    private IHomePresenter mIHomePresenter;
    @BindView(R.id.indicator)
    public TabLayout mTabLayout;
    @BindView(R.id.home_pager)
    public ViewPager mViewPager;
    @BindView(R.id.home_search_box)
    public View mHomeSearchInput;
    @BindView(R.id.qr_scan)
    public View qrScan;
    private HomePagerAdapter mHomePagerAdapter;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_base_home, container, false);
    }

    @Override
    protected void initView(View rootView) {
        mTabLayout.setupWithViewPager(mViewPager);
        mHomePagerAdapter = new HomePagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mHomePagerAdapter);
    }

    @Override
    protected void initListener() {
        mHomeSearchInput.setOnClickListener(v-> {
            LogUtils.d(HomeFragment.this, "home  input click...");
            FragmentActivity activity = getActivity();
            if (activity instanceof MainActivity) {
                ((MainActivity) activity).onHomeSearchClick();
            }
        });
        // 二维码扫描监听
        qrScan.setOnClickListener(v-> {
            startActivity(new Intent(getContext(), ScannerQRActivity.class));
        });
    }
    public interface OnHomeSearchClickListener {
        void onHomeSearchClick();
    }
    @Override
    protected void initPresenter() {
        // 创建Presenter
        mIHomePresenter = PresenterManger.getInstance().getHomePresenter();
        mIHomePresenter.registerViewCallback(this);
    }

    @Override
    protected void loadData() {
        mIHomePresenter.getCategories();
    }

    @Override
    public void onCategoriesLoaded(Categories categories) {
        setUpState(State.SUCCESS);
        LogUtils.d(this, "onCategoriesLoaded");
        if (categories != null) {
            // mViewPager.setOffscreenPageLimit(categories.getData().size()); 加载全部tab
            mHomePagerAdapter.setCategories(categories);
        }
    }

    @Override
    protected void onClickReload() {
        if (mIHomePresenter != null) {
            // 重新获取数据
            mIHomePresenter.getCategories();
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    protected void release() {
        if (mIHomePresenter != null) {
            // 取消回调注册
            mIHomePresenter.unregisterViewCallback(this);
        }
    }
}
